#include "param.h"
#include "log/Logging.h"
#include "configParamUtil.h"
#include "common/httpUtil.h"
#include "accessPublishUtil.h"
#include "siteService/service_site_manager.h"

using namespace servicesite;


bool access_service_hongmei_getDcode(const string ip, qlibc::QData requestData, qlibc::QData& response)
{
    qlibc::QData request_hm;
    request_hm.setString("service_id", "getDcode");
    request_hm.putData("request", requestData);
    LOG_GREEN << "get decode request: " << request_hm.toJsonString();
    bool resultFlag = httpUtil::sitePostRequest(ip, Hongmei_dev_port, request_hm, response);
    LOG_BLUE << "get decode response: " << response.toJsonString();
    return resultFlag;
}


bool access_service_hongmeiCloud(const string &domain, const string &url, qlibc::QData request, qlibc::QData &response)
{
    httplib::SSLClient client(domain);
    client.enable_server_certificate_verification(false);
    client.set_connection_timeout(2, 0);
    client.set_read_timeout(2, 0);
    LOG_GREEN << "hongmeiCloud request: " << request.toJsonString();
    httplib::Result res = client.Post(url, request.toJsonString(), "application/json");
    // 检查响应以及处理结果
    if(res != nullptr && res.error() == Error::Success){ 
        response.setInitData(qlibc::QData(res->body));
        LOG_BLUE << "hongmeiCloud response: " << response.toJsonString();
        return true;
    }else{
        LOG_RED << url <<  " error: " << to_string(res.error());
        return false;
    }
}


void publish_message_config_self_info_update(qlibc::QData content)
{
    //发送面板属性被修改消息
    qlibc::QData publishData;
    publishData.setString("message_id", PANELINFO_MODIFIED_MESSAGE_ID);
    publishData.putData("content", content);
    ServiceSiteManager::getInstance()->publishMessage(PANELINFO_MODIFIED_MESSAGE_ID, publishData.toJsonString());
    LOG_INFO << "publish self_info_update message: " << publishData.toJsonString();
}
